<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>请假办理</title>
<meta name="decorator" content="default" />
<script type="text/javascript">
$(document).ready(function() {
	$(".handle").click(function(){
		var obj = $(this);
		var taskId = obj.data("tid");
		var loanid = obj.data("id");
		
		var tkey=obj.data("tkey");
		//部门领导审批
		if(tkey=="deptLeaderAudit") {
			$.getJSON("${ctx}/bls/loan/detail/" + loanid , function(data){
				 var html= Mustache.render($("#auditTemplate").html(),data);
				 top.$.jBox(html, { title: "流程办理["+obj.data("tname") + "]",buttons:{"同意":"yes","驳回":"no","取消":"cancel"},submit: function (v, h, f) {
					 //同意
					 if(v=="yes") {
							complete(taskId, [{
								key: 'deptLeaderPass',
								value: true,
								type: 'B'
							}]);
					//驳回
					 } else if (v=="no") {
						 top.$.jBox("<div style='padding:10px;'><textarea id='leaderBackReason' style='width: 250px; height: 60px;'></textarea></div>", { title: "请填写驳回理由", submit: function () {
							 var leaderBackReason=top.$("#leaderBackReason").val();
							 //必须填写驳回理由
							 if($.trim(leaderBackReason)=="") {
								 top.$.jBox.error('请填写驳回理由', '错误');
								 return false;
							 } else {
									complete(taskId, [{
										key: 'deptLeaderPass',
										value: false,
										type: 'B'
									}, {
										key: 'leaderBackReason',
										value: leaderBackReason,
										type: 'S'
									}]);
							 }
						 }
						});
					 }
				 }
				 });
			});
		}
		
		//调整申请
		if(tkey=="usertask2") {
			$.getJSON("${ctx}//bls/loan/detail-with-vars/" + loanid + "/" + taskId, function(data){
				 var html= Mustache.render($("#modifyApplyTemplate").html(),data);
				 top.$.jBox(html, { title: "流程办理["+obj.data("tname") + "]",buttons:{"重新申请":"yes","放弃申请":"no","取消":"cancel"},submit: function (v, h, f) {
					 //重新申请或者取消申请
					 var reApply=true;
					 if(v=="no") {
						 reApply=false;
					 }
					 if(v=="yes"|| v=="no") {
						complete(taskId, [{
							key: 'reApply',
							value: reApply,
							type: 'B'
						}, {
							key: 'startdate',
							value: top.$('#modifyApplyContent #startdate').val(),
							type: 'D'
						}, {
							key: 'enddate',
							value: top.$('#modifyApplyContent #enddate').val(),
							type: 'D'
						}, {
							key: 'amount',
							value: top.$('#modifyApplyContent #amount').val(),
							type: 'F'
						}]);
					 } 
				 	}
				 });
				
			});
		}
		
		
	})
});


/**
 * 完成任务
 * @param {Object} taskId
 */
function complete(taskId, variables) {
	// 转换JSON为字符串
    var keys = "", values = "", types = "";
	if (variables) {
		$.each(variables, function() {
			if (keys != "") {
				keys += ",";
				values += ",";
				types += ",";
			}
			keys += this.key;
			values += this.value;
			types += this.type;
		});
	}
	alert(keys+"---"+values+"---"+types);
	// 发送任务完成请求
    $.post('${ctx}/bls/loan/complete/' + taskId, {
        keys: keys,
        values: values,
        types: types
    }, function(resp) {
        if (resp == 'success') {
        	top.$.jBox.tip('任务完成');
            location.reload();
        } else {
        	top.$.jBox.tip('操作失败!');
        }
    });
}
</script>
<script type="text/template" id="auditTemplate">
<table class="table table-striped ">
	<tr>
		<td width="100px;">申请人：</td>
		<td>{{officerid}}</td>
	</tr>
	<tr>
		<td>贷款<font color="red">开始</font>时间：</td>
		<td>{{startdate}}</td>
	</tr>
	<tr>
		<td>贷款<font color="red">结束</font>时间：</td>
		<td>{{enddate}}</td>
	</tr>
	<tr>
		<td>贷款金额：</td>
		<td>{{amount}}</td>
	</tr>
</table>
</script>

<script type="text/template" id="modifyApplyTemplate">
<table class="table table-striped " id="modifyApplyContent">
	<tr>
		<td>部门领导意见：</td>
		<td>
			{{variables.leaderBackReason}}
		</td>
	</tr>
	
	<tr>
		<td width="100px;">申请人：</td>
		<td>{{officerid}}</td>
	</tr>
	<tr>
		<td>贷款<font color="red">开始</font>时间：</td>
		<td>{{startdate}}</td>
	</tr>
	<tr>
		<td>贷款<font color="red">结束</font>时间：</td>
		<td>{{enddate}}</td>
	</tr>
	<tr>
		<td>贷款金额：</td>
		<td>{{amount}}</td>
	</tr>
<tr>
	<td>实际开始时间：</td>
	<td>
		<input id="startdate" readonly="readonly" maxlength="20" class=" Wdate required"
				onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
	</td>
</tr>
<tr>
	<td>实际结束时间：</td>
	<td>
		<input id="enddate" readonly="readonly" maxlength="20" class=" Wdate required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false});"/>
	</td>
</tr>
<tr>
		<td>实际贷款金额：</td>
		<td><input id="amount"  type="text" class="required" /></td>
	</tr>
</table>
</script>
</head>
<body>
	<!-- 
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/oa/workflow/leave/form">请假申请</a></li>
		<li class="active"><a href="${ctx}/oa/workflow/leave/list/task">请假办理</a></li>
		<li><a href="${ctx}/oa/workflow/leave/list/running">正在审批</a></li>
		<li><a href="${ctx}/oa/workflow/leave/list/finished">历史申请</a></li>
	</ul>
	 -->
	<tags:message content="${message}" />
	<table id="contentTable" class="table table-striped table-bordered ">
		<tr>
			<th>所属部门</th>
			<th>信贷人员</th>
			<th>信贷类别</th>
			<th>开始日期</th>
			<th>结束日期</th>
			<th>贷款金额</th>
			<th>贷款余额</th>
			<th>应收利息</th>
			<th>实收利息</th>
			<th>未收利息</th>
			<th>当前节点</th>
			<th>任务创建时间</th>
			<th>流程状态</th>
			<th>操作</th>
		</tr>
		<tbody>
			<c:forEach items="${loans }" var="loan">
				<c:set var="task" value="${loan.task }" />
				<c:set var="pi" value="${loan.processInstance }" />
				<tr id="${loan.id }" tid="${task.id }">
					<td><a href="${ctx}/bls/loan/form?id=${loan.id}">${loan.office.name}</a></td>
					<td>${loan.officerid}</td>
					<td><c:if test="${loan.type eq '1'}">农户</c:if> <c:if
							test="${loan.type eq '2'}">个体工商户</c:if> <c:if
							test="${loan.type eq '3'}">城镇自然人</c:if> <c:if
							test="${loan.type eq '4'}">小企业</c:if> <c:if
							test="${loan.type eq '5'}">其他</c:if></td>
					<td>${loan.startdate}</td>
					<td>${loan.enddate}</td>
					<td>${loan.amount}</td>
					<td>${loan.balance}</td>
					<td>${loan.interes}</td>
					<td>${loan.interestactual}</td>
					<td>${loan.interestbalance}</td>
					<td>${task.name }</td>
					<td>${task.createTime }</td>
					<td>${pi.suspended ? "已挂起" : "正常" }；<b title='流程版本号'>V:
							${loan.processDefinition.version }</b></td>
					<td><c:if test="${empty task.assignee }">
							<a class="claim" href="${ctx }/bls/loan/task/claim/${task.id}">签收</a>
						</c:if> <c:if test="${not empty task.assignee }">
							<%-- 此处用tkey记录当前节点的名称 --%>
							<a class="handle" href="#" data-tkey="${task.taskDefinitionKey}"
								data-tname="${task.name}" data-id="${loan.id}"
								data-tid="${task.id}">办理</a>
						</c:if></td>
				</tr>
			</c:forEach>
		</tbody>
	</table>
</body>
</html>
